#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   __init__.py
@Time    :   2024-07-09 12:58:42
@Author  :   chakcy 
@Email   :   947105045@qq.com
@description   :   中间件
'''

from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI

from .jwt import JWTMiddleware
from .log import LogMiddleware

middleware_list = [
    {
        "middleware": CORSMiddleware, 
        "params": {
            "allow_origins": ["*"], 
            "allow_credentials": True, 
            "allow_methods": ["*"], 
            "allow_headers": ["*"]
        }
    },
    {
        "middleware": LogMiddleware,
        "params": {
            "header_namespace": "Logging"
        }
    },
    {
        "middleware": JWTMiddleware,
        "params": {
            "header_namespace": "Authorization"
        }
    }
]

def add_all_middlewares(app: FastAPI):
    for middleware in middleware_list:
        app.add_middleware(middleware["middleware"], **middleware["params"]) 


__all__ = ["add_all_middlewares"]
